#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const ejs = require('ejs')

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'Project name:',
		default: __dirname.substring(__dirname.lastIndexOf('\\') + 1)
    },
	{
	    type: 'input',
	    name: 'version',
	    message: 'version:',
		default: '1.0.0'
	},
	{
	    type: 'input',
	    name: 'description',
	    message: 'description:'
	},
	{
	    type: 'input',
	    name: 'main',
	    message: 'entry point:',
		default: 'index.js'
	},
	{
	    type: 'input',
	    name: 'author',
	    message: 'author:'
	},
	{
	    type: 'list',
	    name: 'license',
	    message: 'license:',
		choices: [
			'MIT',
			'ISC'
		]
	},
])
.then(answer => {
	// 模板目录
	const tmplDir = path.join(__dirname, 'templates')
	// 目标目录
	const destDir = process.cwd()
	
    fs.readdir(tmplDir, (err, files) => {
        if(err) throw err
        // file是目录下的绝对路径
        files.forEach(file => {
            ejs.renderFile(path.join(tmplDir, file), answer, (err, result) => {
                if(err) throw err
                fs.writeFileSync(path.join(destDir, file), result)
            })
        })
    })
})